home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / randomize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  2.0 KB  |  92 lines

  1. /* randomize.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: randomize.c,v 4.7 1995/02/08 13:14:56 espie Exp $ 
  6.  * $Log: randomize.c,v $
  7.  * Revision 4.7  1995/02/08  13:14:56  espie
  8.  * *** empty log message ***
  9.  *
  10.  * Revision 4.7  1995/02/08  13:14:56  espie
  11.  * *** empty log message ***
  12.  *
  13.  * Revision 4.6  1995/02/01  20:41:45  espie
  14.  * Added color.
  15.  *
  16.  * Revision 4.6  1995/02/01  20:41:45  espie
  17.  * Added color.
  18.  *
  19.  * Revision 4.5  1995/02/01  16:39:04  espie
  20.  * *** empty log message ***
  21.  *
  22.  * Revision 4.5  1995/02/01  16:39:04  espie
  23.  * *** empty log message ***
  24.  *
  25.  */
  26.  
  27. /* input: a series of names (as argv[1:argc - 1])
  28.  * output: the same names, in a random order.
  29.  * with the new database lookup facility, very useful for e.g.,
  30.  * tracker `randomize *` (jukebox)
  31.  */
  32.  
  33. #include "defs.h"
  34.  
  35. ID("$Id: randomize.c,v 4.7 1995/02/08 13:14:56 espie Exp $")
  36.  
  37. /* n = random_range(max): output a number in the range 0:max - 1.
  38.  * For our purpose, we don't have to get a very random number,
  39.  * so the standard generator is alright.
  40.  */
  41. int random_range(max)
  42. int max;
  43.     {
  44.     static init = 0;
  45.  
  46.         /* initialize the generator to an appropriate seed eventually */
  47.     if (!init)
  48.         {
  49.         srand(time(0));
  50.         init = 1;
  51.         }
  52.     return rand()%max;
  53.     }
  54.  
  55. /* output(s): output s in a suitable format. Ideally, output() should use
  56.  * the shell quoting conventions for difficult names. Right now, it doesn't
  57.  */
  58. void output(s)
  59. char *s;
  60.     {
  61.     for(; *s; s++)
  62.         switch(*s)
  63.             {
  64.     /*    case ' ':
  65.         case '(':
  66.         case ')':
  67.         case '\\':
  68.             putchar('\\');
  69.             */
  70.         default:
  71.             putchar(*s);
  72.             }
  73.     putchar(' ');
  74.     }
  75.  
  76. int main(argc, argv)
  77. int argc;
  78. char *argv[];
  79.     {
  80.     int i, k;
  81.  
  82.         /* set up everything so that our names are in argv[0 : argc - 2] */
  83.     for (i = argc - 1, argv++; i; i--)
  84.         {
  85.             /* invariant: the remaining names are in argv[0: i - 1] */
  86.         k = random_range(i);
  87.         output(argv[k]);
  88.         argv[k] = argv[i - 1];
  89.         }
  90.    exit(0);
  91.     }
  92.